﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnrealDebugger.Panels;
using Aga.Controls.Tree;

namespace UnrealDebugger
{
    // Memory operations (Communication from IDE to UDK)

    partial class UnrealDebuggerIDE
    {
        enum MemoryDataTypes
        {
            Bool,
            Int,
            Float,
        }

        public delegate void WriteBooleanValueToMemoryDelegate(bool newValue, string address);
        public event WriteBooleanValueToMemoryDelegate OnWriteBooleanValueToMemory;

        public delegate void WriteIntegerValueToMemoryDelegate(int newValue, string address);
        public event WriteIntegerValueToMemoryDelegate OnWriteIntegerValueToMemory;

        public delegate void WriteFloatValueToMemoryDelegate(float newValue, string address);
        public event WriteFloatValueToMemoryDelegate OnWriteFloatValueToMemory;

        public class Memory
        {
            public static void WriteValueToMemory(string type, string newValue, string address)
            {
                MemoryDataTypes convertedType = MemoryDataTypes.Bool;
                if (Enum.TryParse<MemoryDataTypes>(type, true, out convertedType))
                {
                    switch (convertedType)
                    {
                        case MemoryDataTypes.Bool: WriteBooleanValueToMemory(newValue, address); break;
                        case MemoryDataTypes.Int: WriteIntegerValueToMemory(newValue, address); break;
                        case MemoryDataTypes.Float: WriteFloatValueToMemory(newValue, address); break;
                    }
                }
            }

            protected static void WriteBooleanValueToMemory(string newValue, string address)
            {
                bool convertedNewValue = false;

                bool isValid = true;
                isValid &= Boolean.TryParse(newValue, out convertedNewValue);

                if (isValid)
                {
                    UnrealDebuggerIDE ide = UnrealDebuggerIDE.Instance;
                    if (ide.OnWriteBooleanValueToMemory != null)
                    {
                        ide.OnWriteBooleanValueToMemory(convertedNewValue, address);
                    }
                }
            }

            protected static void WriteIntegerValueToMemory(string newValue, string address)
            {
                int convertedNewValue = 0;

                bool isValid = true;
                isValid &= Int32.TryParse(newValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out convertedNewValue);

                if (isValid)
                {
                    UnrealDebuggerIDE ide = UnrealDebuggerIDE.Instance;
                    if (ide.OnWriteIntegerValueToMemory != null)
                    {
                        ide.OnWriteIntegerValueToMemory(convertedNewValue, address);
                    }
                }
            }

            protected static void WriteFloatValueToMemory(string newValue, string address)
            {
                float convertedNewValue = 0;

                bool isValid = true;
                isValid &= Single.TryParse(newValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out convertedNewValue);

                if (isValid)
                {
                    UnrealDebuggerIDE ide = UnrealDebuggerIDE.Instance;
                    if (ide.OnWriteFloatValueToMemory != null)
                    {
                        ide.OnWriteFloatValueToMemory(convertedNewValue, address);
                    }
                }
            }
        }
    }
}
